package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.BrandMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.service.BrandService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import javafx.util.Builder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    //查询所有
    @Override
    public Result findAll() {
        List<Brand> list = brandMapper.findAll();
        Result result = new Result<>();
        if (list.isEmpty()) {
            result.setMessage("查询失败");
            result.setFlag(false);
            result.setCode(StatusCode.ERROR);
            return result;
        }
        result.setCode(StatusCode.OK);
        result.setFlag(true);
        result.setData(list);
        result.setMessage("查询成功");
        return result;
    }

    //分页查询
    @Override
    public Result findPage(Integer page, Integer size) {
        IPage<Brand> topage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<Brand>(page, size);
        brandMapper.selectPage(topage, null);
        //获取查询的记录
        List<Brand> list = topage.getRecords();
        //获取总条数
        long total = topage.getTotal();
        //放到pageresult中
        PageResult<Brand> tPageResult = new PageResult<Brand>(total, list);
        if (list.isEmpty()) {
            return new Result(false, null, "你查询到的数据为空", null);
        }
        return new Result(true, StatusCode.OK, "success", tPageResult);
    }

    //条件分页查询
    @Override
    public Result<Brand> TfindPage(Integer page, Integer size, Brand brand) {
        IPage<Brand> toPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<Brand>(page, size);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();

        //定义模糊查询条件
        if (brand.getName() != null) {
            wrapper.like(Brand::getName, brand.getName());
        } else if (brand.getImage() != null) {
            wrapper.like(Brand::getImage, brand.getImage());
        } else if (brand.getLetter() != null) {
            wrapper.like(Brand::getLetter, brand.getLetter());
        } else if (brand.getSeq() != null) {
            wrapper.like(Brand::getSeq, brand.getSeq());
        }

        brandMapper.selectPage(toPage, wrapper);
        //获取查询的记录
        List<Brand> list = toPage.getRecords();
        //判断list集合中查没查到数据
        if (list.isEmpty()) {
            return new Result(false, null, "你查询到的数据为空", null);
        }
        //获取分页查询总条数
        long total = toPage.getTotal();
        //把数据放到pageresult中
        PageResult<Brand> brandPageResult = new PageResult<>(total, list);
        return new Result(true, StatusCode.OK, "success", brandPageResult);
    }

    //根据id查询
    @Override
    public Result<Brand> findById(Integer id) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Brand::getId, id);
        Brand brand = brandMapper.selectOne(wrapper);
        if (brand == null) {
            return new Result(false, null, "error", null);
        }
        return new Result(true, StatusCode.OK, "success", brand);
    }

    //根据id修改
    @Override
    public Result updateById1(Brand brand, Integer id) {
        brand.setId(id);
        int i = brandMapper.updateById(brand);
        if (i > 0) {
            return new Result(true, StatusCode.OK, "success", null);
        }
        return new Result(false, StatusCode.ERROR, "error", null);

    }

    //根据id删除
    @Override
    public Result deleteById(Integer id) {
        int i = brandMapper.deleteById(id);
        if (i > 0) {
            return new Result(true, StatusCode.OK, "success", null);
        }
        return new Result(false, StatusCode.ERROR, "error", null);
    }

    //添加品牌
    @Override
    public Result addBrand(Brand brand) {
        baseMapper.insert(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }


}
